import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/AppNavigator.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/common/C.dart';
import 'package:huajianshe_app/common/TS.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/network/response/BaseResp.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';
import 'package:huajianshe_app/widget/SimpleScrollView.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';
import 'package:oktoast/oktoast.dart';

class LeaveMessageDialog extends StatefulWidget {

  final int? circleId;

  const LeaveMessageDialog(this.circleId, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return LeaveMessageDialogState();
  }
}

class LeaveMessageDialogState extends BaseState<LeaveMessageDialog> {
  TextEditingController editingController = TextEditingController();
  FocusNode inputNode = FocusNode();
  @override
  Widget build(BuildContext context) {
    print('circleId: ${widget.circleId}');
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: LayoutBuilder(builder: (context, constraints) {
        return SingleChildScrollView(
            child: ConstrainedBox(constraints: BoxConstraints(minWidth: constraints.maxWidth, minHeight: constraints.maxHeight),
                child: IntrinsicHeight(
                    child: Column(
                        children: [
                          Spacer(),
                          RoundedCornerWidget(backgroundColor: Colors.white, radius: 13, borderWidth: 0, child: Column(
                              children: [
                                  SizedBox(height: 45, child: Row(
                                      children: [
                                        W(15),
                                        Text('动态详情', style: TS(15, C.gray1A)),
                                        Spacer(),
                                        SmartButton(width: 20, height: 20, drawableLeft: LoadImage('ic_close'), backgroundColor: Colors.transparent, borderColor: Colors.transparent,
                                          onPressed: () { AppNavigator.pop(context); }),
                                        W(15)
                                      ]
                                  )),
                                  D(),
                                  Padding(padding: EdgeInsets.symmetric(vertical: 12, horizontal: 15),
                                      child: TextField(autofocus: false, controller: editingController, focusNode: inputNode,
                                        maxLines: 5, maxLength: 100,
                                        decoration: InputDecoration(
                                          isDense: true,
                                          contentPadding: EdgeInsets.only(left: 15, right: 15, top: 10, bottom: 10),
                                          fillColor: C.grayF6,
                                          filled: true,
                                          border: new OutlineInputBorder(
                                            borderSide: BorderSide.none,
                                            borderRadius: const BorderRadius.all(Radius.circular(9.0)),
                                          ),
                                          hintStyle: TextStyle(color: Color(0xffcccccc)),
                                          hintText: "说点什么吧",
                                        ),
                                      )
                                  ),
                                  SmartButton(text: '发送留言', fontSize: 15, height: 44, width: double.infinity, radius: 13, cornerPosition: 12, onPressed: send,)
                              ]
                          ))
                        ]
                    )
                  ),
                )
            );
      })
    );
  }

  send() {
    String text = editingController.text;
    if (text.isNotEmpty && widget.circleId != null) {
      var data = {
        'circle_id': widget.circleId,
        'content': text
      };
      sendReq(Api.leaveMessage(), BaseResp(), data: data);
    }
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    if (t is BaseResp) {
      showToast(t.msg ?? '操作成功');
      editingController.text = '';
    }
  }
}